﻿using System;
using System.IO;
using System.Windows.Forms;
using Business.Entity.Fiscal.ClassesRelacionadas;
using NHibernate;

namespace Business.InformacoesIniciais
{
    public class DadosIniciaisCfop
    {
        public static void Iniciar()
        {
            var s = CfopRepository.GetSession();
            var t = s.BeginTransaction();
            try
            {
                var dir = Environment.CurrentDirectory + "\\arquivos\\";
                var arqCfop = new StreamReader(dir + "cfop.txt");
                var line = "";
                while (line != null)
                {
                    line = arqCfop.ReadLine();
                    if (line != null)
                    {
                        var split = line.Split(';');
                        var cfop = new CFOP()
                        {
                            Cfop = int.Parse(split[0]),
                            Aplicacao = split[1]
                        };
                        s.Save(cfop);
                    }
                }
                t.Commit();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro ao importar a tabela CFOP.\n" + ex.Message);
                t.Rollback();
            }
        }
    }
}
